/*
 * Copyright 2010 J Patrick Davenport
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v4view.web.ext;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import v4view.web.Script;
import v4view.web.ScriptTests;

public class ResourceBackedScriptTest extends ScriptTests {

	@Test
	public void checkConstructor() {
		final ResourceBackedScript script = new ResourceBackedScript();
		assertTrue(script.hasBeforeRenderCallbacks());
	}

	@Override
	protected Script getScript() {
		return new ResourceBackedScript();
	}

	@Override
	protected Script getScript(final boolean _autoAppend) {
		throw new UnsupportedOperationException("You can't append to a script that doesn't allow src.");
	}

	@Override
	@Test(expected = UnsupportedOperationException.class)
	public void checkSrc() {
		super.checkSrc();
	}

	@Override
	@Test(expected = UnsupportedOperationException.class)
	public void checkBooleanConstruction() {
		super.checkBooleanConstruction();
	}

	@Override
	@Test
	public void checkRender() throws IOException {
		final ResourceBackedScript script = (ResourceBackedScript) this.getScript();
		script.setScriptBodyLocation("testing.js");
		final String expectedRender = "<script>//<![CDATA[\r\nfunction littleMiss() {\r\n" +
				"\treturn cantBeWrong == true;\r\n" +
				"}\r\n" +
				"//]]</script>";
		final MockHttpServletResponse res = new MockHttpServletResponse();
		script.render(new MockHttpServletRequest(), res);
		assertEquals(expectedRender, res.getContentAsString());
	}
}
